<?php
class BaseAction extends Action
{
    function _initialize()
    {

    }

    /**
     * 操作错误跳转的快捷方法
     * @access protected
     * @param string $message 错误信息
     * @param Boolean $ajax 是否为Ajax方式
     * @return void
     */
    protected function error($message, $ajax = false, $isSend = true)
    {
        $userInfo = getLoginUserInfo();
        if ($userInfo['id'] != 1 && $isSend) {
            Vendor('PHPMailer.SendMail');
            $mail = new SendMail();
            $mailTitle = '管理平台错误:' . date('Y-m-d G:i:s') . '用户：' .
                $userInfo['id'] . ' ' . $userInfo['name'] . ' ' . MODULE_NAME . ' ' . ACTION_NAME;
            $mailBody = $mailTitle . '<hr/>' . 'GROUP_NAME: ' . GROUP_NAME .
                '<br/>MODULE_NAME: ' . MODULE_NAME . '<br/>ACTION_NAME: ' . ACTION_NAME .
                '<br/>错误信息：' . $message;
            $mail->setParameAndSend($mailTitle, $mailBody, 'iceinto@163.com');
        }
        parent::error($message, $ajax);
    }

    /**
     * 操作错误提示信息
     * @param String $message
     * @author Ice <iceinto@mallog.com.cn>
     */
    protected function errorMessage($message)
    {
        if ($message) {
            $this->assign('errorMessage', $message);
        }
    }

    /**
     * 操作成功提示信息
     * @param String $message
     * @author Ice <iceinto@mallog.com.cn>
     */
    protected function successMessage($message)
    {
        if ($message) {
            $this->assign('successMessage', $message);
        }
    }

    /**
     * 操作提示信息
     * @param String $message
     * @author Ice <iceinto@mallog.com.cn>
     */
    protected function noticeMessage($message)
    {
        if ($message) {
            $this->assign('noticeMessage', $message);
        }
    }

    /**
     * 执行文件上传功能
     * @param String $type 上传类型
     * @param String $IsDate 是否根据时间生成目录
     * @author Ice <iceinto@mallog.com.cn>
     */
    protected function uploadFiles($type = '', $isDate = false)
    {
        /**
         * 'name' => string 'dddddddddddddddddddddddddddddddddd.jpg' (length=38)
         * 'type' => string 'image/jpeg' (length=10)
         * 'size' => int 625330
         * 'key' => int 0
         * 'extension' => string 'jpg' (length=3)
         * 'savepath' => string 'E:/wamp/www/uploads/brand/' (length=26)
         * 'savename' => string '4dd0ddd87c495.jpg' (length=17)
         * 'hash' => string '6b238238328f6e407cf58ca0a0efe22d' (length=32)
         */
        $user = getLoginUserInfo();
        $info = null;
        import("@.ORG.Net.UploadFile");
        $upload = new UploadFile(); // 实例化上传类
        $upload->maxSize = 3145728; // 设置附件上传大小
        $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        $upload->saveRule = 'uniqid';
        if ($type != '' && $isDate) {
            //执行品牌图片上传
            $y = date('Y');
            $m = date('m');
            $d = date('d');
            $savePath = UPLOAD_PATH . $type . '/' . $y . '/' . $m . $d . '/';
            //循环生成上面目录
            mk_dir($savePath);
            $upload->savePath = $savePath;
        } else if ($type != '') {
            $upload->savePath = UPLOAD_PATH . $type . '/';
        } else {
            $upload->savePath = UPLOAD_PATH; // 设置附件上传目录
        }
        if (!$upload->upload()) {
            // 上传错误提示错误信息
            $this->error($upload->getErrorMsg());
        } else { // 上传成功 获取上传文件信息
            $info = $upload->getUploadFileInfo();
            //执行数据库信息保存
            $FileManagedModel = M('FileManaged' . ucwords($type));
            $data['uid'] = $user['id'];
            $data['name'] = $info[0]['name'];
            $data['uri'] = $info[0]['savename'];
            $data['mime'] = $info[0]['type'];
            $data['size'] = $info[0]['size'];
            $data['time'] = time();
            $data['type'] = $type;
            $info['id'] = $FileManagedModel->add($data);
            //dump($FileManagedModel->getLastSql());
        }
        return $info['id'];
    }

    /**
     * 根据图片标示上传图片
     * @param Int $fId    文件标识
     * @param String $type 上传类型
     * @author Ice <iceinto@mallog.com.cn>
     */
    protected function delUpLoadFile($fId, $type = '', $isDate = false)
    {
        //废除操作
    }

    /**
     * 根据类型获取图片完整显示
     * @param String $type 上传类型
     * @param String $isDate 是否有时间设置
     * @return String 图片地址
     * @author Ice <iceinto@mallog.com.cn>
     */
    protected function getManagedUrl($id, $type = '', $isDate = false)
    {
        $defaultImages = UPLOAD_URL . 'default.jpg';
        $FileManagedModel = M('FileManaged' . ucwords($type));
        $map = array();
        if ($id) {
            $map['id'] = $id;
            $fImages = $FileManagedModel->where($map)->find();
            //dump($FileManagedModel->getLastSql());
            //dump($fImages);
            if ($fImages) {
                if ($type && !$isDate) {
                    return UPLOAD_URL . $type . '/' . $fImages['uri'];
                } else if ($type && $isDate) {
                    //解析时间
                    $year = strftime('%Y', $fImages['time']);
                    $date = strftime('%m%d', $fImages['time']);
                    //返回时间地址
                    return UPLOAD_URL . $type . '/' . $year .
                        '/' . $date . '/' . $fImages['uri'];
                } else {
                    return UPLOAD_URL . $fImages['uri'];
                }
            }
            return $defaultImages;
        }
        return $defaultImages;
    }

    /**
     * 获取分页信息
     * @param Int $count 总条数
     * @author Ice <iceinto@mallog.com.cn>
     * @return class Page
     */
    protected function getPage($count)
    {
        return new Page(intval($count), 18);
    }

    /**
     * 设置基本数据
     * @param String $model 模式名称
     * @param Array $map 查询条件
     * @param String $name 模板存放名称
     * @param Blooean $more 是否显示更多
     * @author Ice <iceinto@mallog.com.cn>
     */
    protected function setBaseData($model, $map, $name, $more = false, $field = '')
    {
        $BaseModel = M($model);
        if ($more) {
            $data = $BaseModel->where($map)
                ->field($field)
                ->select();
        } else {
            $data = $BaseModel->where($map)
                ->field($field)
                ->find();
        }
        //dump($BaseModel->getLastSql());exit;
        if ($data) {
            $this->assign($name, $data);
            return $data;
        } else {
            $this->error($model . '数据不存在 或 数据传递有错误！');
        }
    }

    /**
     * 对数据自动验证进行封装
     * @param String $model Model名称
     * @param Array $data 处理的数据集
     * @author Ice <iceinto@mallog.com.cn>
     */
    protected function createAdd($model, $data = '', $isPrompt = true)
    {
        $Model = D($model);
        $crs = $Model->create($data);
        if ($crs) {
            $rs = $Model->add();
            if ($rs) {
                if ($isPrompt) {
                    $this->successMessage('添加成功！');
                }
                return $rs;
            } else {
                if ($isPrompt) {
                    $this->error('添加失败！' . $Model->getError());
                }
                return false;
            }
        } else {
            if ($isPrompt) {
                $this->error($Model->getError());
            }
        }
        return false;
    }

    /**
     * 对数据自动验证进行封装
     * @param String $model Model名称
     * @param Array $data 处理的数据集
     * @author Ice <iceinto@mallog.com.cn>
     */
    protected function createUpdate($model, $data = '', $isPrompt = true)
    {
        $Model = D($model);
        $crs = $Model->create($data);
        if ($crs) {
            $rs = $Model->save();
            if ($rs) {
                $this->successMessage('更新成功！');
                return true;
            } else {
                if ($isPrompt) {
                    $this->error('更新失败！' . $Model->getError());
                }
                return false;
            }
        } else {
            if ($isPrompt) {
                $this->error($Model->getError());
            }
        }
        return false;
    }

    public function getMall($mid)
    {
        $mall = M('Market')->field('id,name')->find($mid);
        if (!$mall) {
            $this->error('传递商场信息有错误！');
        }
        return $mall;
    }

    public function getBrand($bid)
    {
        $brand = M('Brand')->field('id,name')->find($bid);
        if (!$brand) {
            $this->error('传递品牌信息有错误！');
        }
        return $brand;
    }

    public function getFloor($mid, $fid)
    {
        $map = array();
        $map['mid'] = $mid;
        $map['fid'] = $fid;
        $floor = M('Floor')->where($map)->field('id,sain,name')->find();
        if (!$floor) {
            $this->error('传递楼层信息有错误！');
        }
        return $floor;
    }
}

?>